﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HUGATE.ItemObject;
using CoreLibs;
using HUGATE.Utility;

namespace HUGATE.Item.Controls.Product
{

    /// <summary>
    /// Creator : thai huy
    /// Date    : 14/12/2010
    /// Desc    : Sản phẩm
    /// </summary>

    public partial class ProductList : UserControlBase
    {

        #region Variables

        private int iPageSize = 10;
        private string strReturnPage = "";
        private string strEditPage = "ProductEdit.aspx";

        #endregion

        #region Properties

        public int PageSize
        {
            get { return iPageSize; }
            set { iPageSize = value; }
        }
        public string EditPage
        {
            get
            {
                return strEditPage;
            }
            set
            {
                strEditPage = value;
            }
        }

        #endregion

        #region Permissions


        #endregion

        #region Support Functions

        /// <summary>
        /// Nạp danh sách dữ liệu vào lưới
        /// </summary>
        /// <param name="intPage"></param>
        private void GridDatabind(int intPage)
        {
            if (txtSearch.Text == HUGATE.Utility.ResxManager.GetString("SearchString") + "...")
            {
                txtSearch.Text = "";
            }
            ITEMProduct objProduct = new ITEMProduct();
            DataTable view = objProduct.SearchData(
                   txtSearch.Text,
                Convert.ToInt64(txtCategoryNameS.Text.Trim() == string.Empty ? "0" : btnChooseCategory.CommandArgument),
                Convert.ToInt64(txtManufacturerNameS.Text.Trim() == string.Empty ? "0" : btnChooseManufacturer.CommandArgument),
                Convert.ToInt64(txtUnitNameS.Text.Trim() == string.Empty ? "0" : btnChooseUnit.CommandArgument),
                Convert.ToInt64(txtProductStatusNameS.Text.Trim() == string.Empty ? "0" : btnChoseProductStatus.CommandArgument));
            grvObject.DataSource = view;
            grvObject.PageIndex = intPage;
            grvObject.DataBind();

        }
        /// <summary>
        /// Nạp dữ liệu vào Search Oject
        /// </summary>
        private void AssignedSOAssignSource(CommonControls.Search.SearchObject Oject, string ClCaption1, string ClCaption2, string ClID, string ClName1, string ClName2, string Store, string keyword)
        {
            Oject.ColumnCaption1 = ClCaption1;
            Oject.ColumnCaption2 = ClCaption2;
            Oject.ColumnID = ClID;
            Oject.ColumnName1 = ClName1;
            Oject.ColumnName2 = ClName2;
            Oject.StoreName = Store;
            Oject.ParaSearch1 = keyword;
            Oject.MultiSelect = false;
        }
        #endregion

        #region Event Functions

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                btnAddNew.Text = HUGATE.Utility.ResxManager.GetString("Btn_AddNew");
                btnReturn.Text = HUGATE.Utility.ResxManager.GetString("Btn_Return");
                btnSearch.Text = HUGATE.Utility.ResxManager.GetString("Btn_Search");

                grvObject.Columns[0].HeaderText = HUGATE.Utility.ResxManager.GetString("ID") + HUGATE.Utility.ResxManager.GetString("ITEMProduct");
                grvObject.Columns[1].HeaderText = HUGATE.Utility.ResxManager.GetString("Name") + HUGATE.Utility.ResxManager.GetString("ITEMProduct");
                grvObject.Columns[2].HeaderText = HUGATE.Utility.ResxManager.GetString("Name") + HUGATE.Utility.ResxManager.GetString("ITEMCategory");
                grvObject.Columns[3].HeaderText = HUGATE.Utility.ResxManager.GetString("Name") + HUGATE.Utility.ResxManager.GetString("ITEMManufacturer");
                grvObject.Columns[4].HeaderText = HUGATE.Utility.ResxManager.GetString("Name") + HUGATE.Utility.ResxManager.GetString("ITEMUnit");
                grvObject.Columns[5].HeaderText = HUGATE.Utility.ResxManager.GetString("Name") + HUGATE.Utility.ResxManager.GetString("ITEMProductStatus");
                grvObject.Columns[6].HeaderText = HUGATE.Utility.ResxManager.GetString("VAT");
                grvObject.Columns[7].HeaderText = HUGATE.Utility.ResxManager.GetString("VATPercent");
                grvObject.Columns[8].HeaderText = HUGATE.Utility.ResxManager.GetString("Description");
                grvObject.Columns[9].HeaderText = HUGATE.Utility.ResxManager.GetString("Serial");
                grvObject.Columns[10].HeaderText = HUGATE.Utility.ResxManager.GetString("Actived");
                grvObject.Columns[11].HeaderText = HUGATE.Utility.ResxManager.GetString("Image");

                txtSearch.Attributes.Add("value", HUGATE.Utility.ResxManager.GetString("SearchString") + "...");
                txtSearch.Attributes.Add("onfocus", "ObjOnFocus(this,'" + HUGATE.Utility.ResxManager.GetString("SearchString") + "...', false)");
                txtSearch.Attributes.Add("onblur", "ObjOnBlur(this,'" + HUGATE.Utility.ResxManager.GetString("SearchString") + "...', false)");
                btnReturn.OnClientClick = "window.location='" + Globals.ApplicationVRoot() + "/" + strReturnPage + "';return false;";

                GridDatabind(0);
            }
            ctlParentCategory.GetString += new Category.ParentCategory.GetNameParentID(ctlParentCategory_GetString);
            AssignedSOAssignSource(SOManufacturer, "Mã Nhà Sản Xuất", "Tên Nhà Sản Xuất", "ManufacturerID", "ManufacturerID", "ManufacturerName", "ITEM_Manufacturer_SRH", "@KeyWord");
            SOManufacturer.Selected += new EventHandler(SOManufacturer_Selected);
            AssignedSOAssignSource(SOUnit, "Mã Đơn Vị", "Tên Đơn Vị", "UnitID", "UnitID", "UnitName", "ITEM_Unit_SRH", "@KeyWord");
            SOUnit.Selected += new EventHandler(SOUnit_Selected);
            AssignedSOAssignSource(SOProductStatus, "Mã Trạng thái sản phẩm", "Tên Trạng thái sản phẩm", "ProductStatusID", "ProductStatusID", "ProductStatusName", "ITEM_ProductStatus_SRH", "@KeyWord");
            SOProductStatus.Selected += new EventHandler(SOProductStatus_Selected);
        }

        void ctlParentCategory_GetString(object sender, EventArgs e)
        {
            if (ctlParentCategory.temp != null)
            {
                DataRow objRow = sender as DataRow;
                txtCategoryNameS.Text = Convert.ToString(objRow["CategoryName"]);
                btnChooseCategory.CommandArgument = objRow["CategoryID"].ToString();
            }
            mpeCaragory.Hide();
            udpCategory.Update();
        }
        private void SOManufacturer_Selected(object sender, EventArgs e)
        {
            DataTable dt = SOManufacturer.dtResult;
            txtManufacturerNameS.Text = dt.Rows[0]["ManufacturerName"].ToString();
            btnChooseManufacturer.CommandArgument = dt.Rows[0]["ManufacturerID"].ToString();
            mpeManufacturer.Hide();
            udpManufacturer.Update();
        }

        private void SOUnit_Selected(object sender, EventArgs e)
        {
            DataTable dt = SOUnit.dtResult;
            txtUnitNameS.Text = dt.Rows[0]["UnitName"].ToString();
            btnChooseUnit.CommandArgument = dt.Rows[0]["UnitID"].ToString();
            mpeUnit.Hide();
            udpUnit.Update();
        }
        private void SOProductStatus_Selected(object sender, EventArgs e)
        {
            DataTable dt = SOProductStatus.dtResult;
            txtProductStatusNameS.Text = dt.Rows[0]["ProductStatusName"].ToString();
            btnChoseProductStatus.CommandArgument = dt.Rows[0]["ProductStatusID"].ToString();
            mpeProductStatus.Hide();
            udpProductStatus.Update();
        }
        protected void ibtClearCategory_Click(object sender, ImageClickEventArgs e)
        {
            txtCategoryNameS.Text = "";
            btnChooseCategory.CommandArgument = "";
            udpCategory.Update();
        }

        protected void ibtClearManufacturer_Click(object sender, ImageClickEventArgs e)
        {
            txtManufacturerNameS.Text = "";
            btnChooseManufacturer.CommandArgument = "";
            udpManufacturer.Update();
        }

        protected void ibtClearUnit_Click(object sender, ImageClickEventArgs e)
        {
            txtUnitNameS.Text = "";
            btnChooseUnit.CommandArgument = "";
            udpUnit.Update();
        }
        protected void ibtClearProductStatus_Click(object sender, ImageClickEventArgs e)
        {
            txtProductStatusNameS.Text = "";
            btnChoseProductStatus.CommandArgument = "";
            udpProductStatus.Update();
        }

        protected void btnSearch_Click1(object sender, EventArgs e)
        {
            GridDatabind(0);
        }

        protected void grvObject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridDatabind(e.NewPageIndex);
        }

        protected void imgDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbtTemp = sender as LinkButton;
            ITEMProduct objProduct = new ITEMProduct();
            objProduct.ProductID = Convert.ToString(lbtTemp.CommandArgument);
            objProduct.CreatedUser = User.UserName;
            objProduct.DeletedUser = User.UserName;
            objProduct.DeletedDate = DateTime.Now;
            if (objProduct.Delete())
            {
                GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("DeleteItemSuccessful"), HUGATE.Utility.ResxManager.GetString("ITEMProduct", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.Information);
                GridDatabind(grvObject.PageIndex);
            }
            else
                GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("CouldNotDeleteParent"), HUGATE.Utility.ResxManager.GetString("ITEMProduct", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.WarningMessage);
        }
        protected void lbtproduct_Click(object sender, EventArgs e)
        {
            Session["NewProduct"] = ((LinkButton)sender).Text;
            Response.Redirect(EditPage + "?productid=" + ((LinkButton)sender).Text);
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(EditPage);
        }


        #endregion Event Functions

        protected void grvObject_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            GridDatabind(e.NewPageIndex);
        }
        protected string GetProductImage(object obj)
        {
            DataRowView drv = obj as DataRowView;
            string strFileName = "NoImage.jpg";
            if (!Convert.IsDBNull(drv["Image"]) && !string.IsNullOrEmpty(drv["Image"].ToString()))
            {
                strFileName = drv["Image"].ToString();
            }

            string strImageThumb = Globals.GetConfig("GEN_ThumbnailImage");
            if (string.IsNullOrEmpty(strImageThumb))
                strImageThumb = "120,120";
            string[] strImageSize = strImageThumb.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            return "<a href=\"" + Globals.ApplicationVRoot() + "/ImageHandler.ashx?m=" + WebLibs.EncryptDecrypt.EncryptRijinDael("ERP.Item.Product", Globals.GetConfig("SEC_FileSecureCode")) + "&f=" + WebLibs.EncryptDecrypt.EncryptRijinDael(strFileName, Globals.GetConfig("SEC_FileSecureCode"))
                + "\" target=\"_blank\"><img style='text-align:center;' src=\"" + Globals.ApplicationVRoot() + "/ImageHandler.ashx?m=" + WebLibs.EncryptDecrypt.EncryptRijinDael("ERP.Item.Product", Globals.GetConfig("SEC_FileSecureCode")) + "&f=" + WebLibs.EncryptDecrypt.EncryptRijinDael(strFileName, Globals.GetConfig("SEC_FileSecureCode"))
                + "\" style=\"border:0\" alt=\"" + strFileName + "\" width=\"" + strImageSize[0] + "\" /></a>";
        }
    }
}
